<?php

namespace app\common\resourceHandle;

use think\Model;

abstract class ResourceHandle
{

    abstract function handle(Model $model);

    public function handleCollection($data, $method = 'handle'): array
    {
        $res = [];
        foreach ($data as &$item) {
            if (!empty($item)) {
                $res[] = $this->$method($item);
            }
        }
        return $res;
    }

    /**
     * 数据上提
     * @param array $needUp
     * @param array $data
     * @param string $field
     * @return void
     */
    public function dataUp(array $needUp, array &$data, string $field)
    {
        if(empty($data)) return;
        foreach ($data as &$d) {
            foreach ($needUp as $need) {
                $d[$need] = $d[$field][$need] ?? '';
            }
            unset($d[$field]);
        }
    }
}